/*
https://leetcode-cn.com/problems/candy/solution/fen-fa-tang-guo-by-leetcode-solution-f01p/
 */
public class Solution135 {
    public int candy(int[] ratings) {
        int[] f=new int[ratings.length];
        f[0]=1;
        for (int i=1;i<ratings.length;i++){
            if (ratings[i]>ratings[i-1]){
                f[i]=f[i-1]+1;
            }else{
                f[i]=1;
            }
        }
        int[] g=new int[ratings.length];
        g[ratings.length-1]=1;
        for (int i=ratings.length-2;i>=0;i--){
            if (ratings[i]>ratings[i+1]){
                g[i]=g[i+1]+1;
            }else{
                g[i]=1;
            }
        }
        int ans=0;
        for (int i=0;i<ratings.length;i++){
            ans=ans+Math.max(g[i],f[i]);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution135().candy(new int[]{1,0,2}));
    }
}
